<?php 
/* 
Template Name: Product manager
*/
?>

<?php 
 if ( !is_user_logged_in() ) 
    {
      ?>

        
              <?php wp_die( __( 'Vui lòng đăng nhập hoặc quay lai trang chủ ! ' ) ); ?>
       
         
     <?php 
          
    }//if ( ! current_user_can( 'list_users' ) ) 
    elseif (is_user_logged_in() )  
 { 
 get_header(); 

    if ( ( $current_user->roles[0] == 'author') or ( $current_user->roles[0] == 'contributor') or ( $current_user->roles[0] == 'subscriber') ) 
      {
       ?>

        <div id='content_nano_two' class='clearfix' style='min-height:300px;'>
              <h1 style='color:#000;'>bạn không được phép truy cập trang này </h1>
              <?php wp_die( __( 'Vui lòng đăng nhập hoặc quay lai trang chủ ! ' ) ); ?>
        </div>
         
     <?php 

      }//if ( ( $current_user->roles[0] == 'author') or ( $current_user->roles[0] == 'contributor') or ( $current_user->roles[0] == 'subscriber') ) 
      elseif ( ( $current_user->roles[0] != 'author') or ( $current_user->roles[0] != 'contributor') or ( $current_user->roles[0] != 'subscriber') )
      {
     
 if(( $current_user->roles[0] == 'editor'))
        {
          ?>
              <div id="content_nano_two" class="clearfix">
    
                <?php 
        // $query_user = "SELECT * FROM wp_may INNER JOIN wp_user_wp_may ON wp_may.id_may=wp_user_wp_may.ID; " ;
    $sql=@"SELECT  wp_users.ID,wp_users.user_login,wp_may.*
           FROM wp_user_wp_may

      INNER JOIN wp_may 

      ON wp_user_wp_may.id_may = wp_may.id_may

      JOIN wp_users

      ON wp_user_wp_may.ID = wp_users.ID

      where wp_user_wp_may.ID=".$current_user->ID;
                 
                    ?>            

                    <div id='maycanphanquyen' style='margin: 30px 0px 10px 0px; min-height: 300px;'>
                                      <form >
                                                      <table class="wp-list-table widefat fixed users" cellspacing="0">
                                                        <thead>
                                                          <tr>
                                                            <th scope="col" id="cb" class="manage-column column-cb check-column" style=""> </th>
                                                            <th scope="col" id="username"  style="text-align: left;">
                                                                <span>Ảnh</span>
                                                            </th>
                                                            <th scope="col" id="name"  style="text-align: left;">
                                                            <span>Tên máy</span>
                                                            </th>
                                                            <th scope="col" id="email" style=""></th>
                                                          </tr>
                                                        </thead>

                                                        <tfoot>
                                                        <tr>
                                                          <th scope="col" style=""> </th>
                                                          <th scope="col"  style="text-align: left;">
                                                              <span>Ảnh</span>
                                                          </th>
                                                          <th scope="col" style="text-align: left;">
                                                              <span >Tên máy</span>
                                                          </th>
                                                          <th scope="col" sstyle="text-align: left;position: relative;float: right;">
                                                          </th>
                                                        </tr>
                                                        </tfoot>

                                                        <tbody id="the-list" data-wp-lists="list:user">

                                                      <?php 
                                                         //$rows = $wpdb->get_results($query_user);
                                                         //
                                                         $rows = $wpdb->get_results($sql);
                                                        
                                                        foreach($rows as $post)
                                                          //var_dump($post);
                                                        {

                                                      ?>
                                                        <tr id="product-<?php $post->id_may; ?>" class="alternate">

                                                          <th scope="row" class="check-column"></th>

                                                          <td >

                                                           <?php if ($post->image_may_url !=='') 
                                                              {
                                                                ?>
                                                                  <img alt="<?php echo $post->name_may;?>" src="<?php echo get_bloginfo('template_directory').'/upload_may/images/'.$post->image_may_url;?>" class="avatar avatar-32 photo" height="40" width="40" style=""> 
                                                                <?php
                                                              }else { ?>
                                                                      <img style='height=:40px; width:40px;' 
                                                                      src="<?php echo get_bloginfo('template_directory').'/images/no_image.gif';?>">
                                                               <?php } ?>
                                                               
                                                           <td class="name column-name" style="text-align: left;font-weight: bold;"><?php echo $post->name_may;?></td>
                                                            <td class="email column-email" style="display: inline-block;width: 297px;">
                                                                   <form><fieldset></fieldset></form>
                                                           <div style="display: inline-block;width: 300px;padding-top: 11px;">
                                                            
                                                            <div style='float:left;'>
                                                             <form action='<?php bloginfo('url');?>/manager-product-set?id=<?php echo $post->id_may;?>' method='POST'>
                                                              
                                                          <input type='hidden' name='phanquyen' id='phanquyen' value='<?php echo $post->id_may;?>'/>
                                                          <input  class="edit" type='submit' name='phanquyen_sumit' value='Phân quyền'/>
                                                          
                                                            </form>
                                                          </div>

                                                          </div>

                                                            </td>


                                                         
                                                       </tr><!-- id="user-xyz" class="alternate" -->

                                                      <?php } // end foreach($rows as $post) ?>


                                                       </tbody>
                                                      </table>
                  
                             </form>
                  </div><!-- id='maycanphanquyen' -->
                  


                </div><!-- id="content_nano_two" -->

          <?php 
        }// neu la editor

        if (( $current_user->roles[0] == 'administrator')) 
        {
          ?>
            <div id="content_nano_two" class="clearfix">
              <!--
                <a  href='<?php bloginfo('url');?>/add-product'> 
                  <button  style='color: #555;' class="button action" id='them_moi_nhieu'> Thêm máy </button>
                </a>
              -->
              <a  href='<?php bloginfo('url');?>/ckeditor_upload'> 
                  <button  style='color: #555;' class="button action" id='them_moi_nhieu'> Thêm máy </button>
                </a>
              <div id='quanlimay'>    
                <?php 
                   $query_user = "SELECT * FROM wp_may" ;
                                       
                    ?>            
                                      <form >
                                                      <table class="wp-list-table widefat fixed users" cellspacing="0">
                                                        <thead>
                                                          <tr>
                                                            <th scope="col" id="cb" class="manage-column column-cb check-column" style=""> </th>
                                                            <th scope="col" id="username"  style="text-align: left;">
                                                                <span>Ảnh</span>
                                                            </th>
                                                            <th scope="col" id="name"  style="text-align: left;">
                                                            <span>Tên máy</span>
                                                            </th>
                                                            <th scope="col" id="email" style=""></th>
                                                          </tr>
                                                        </thead>

                                                        <tfoot>
                                                        <tr>
                                                          <th scope="col" style=""> </th>
                                                          <th scope="col"  style="text-align: left;">
                                                              <span>Ảnh</span>
                                                          </th>
                                                          <th scope="col" style="text-align: left;">
                                                              <span >Tên máy</span>
                                                          </th>
                                                          <th scope="col" sstyle="text-align: left;position: relative;float: right;text-align: left;">
                                                          </th>
                                                        </tr>
                                                        </tfoot>

                                                        <tbody id="the-list" data-wp-lists="list:user">

                                                      <?php 
                                                         $rows = $wpdb->get_results($query_user);
                                                        foreach($rows as $post)
                                                          //var_dump($post);
                                                        {

                                                      ?>
                                                        <tr id="product-<?php $post->id_may; ?>" class="alternate">

                                                          <th scope="row" class="check-column"></th>

                                                          <td >

                                                        <?php if ($post->image_may_url =='')
                                                         {
                                                          ?>
                                                           <img alt="<?php echo $post->name_may;?>" src="<?php echo get_bloginfo('template_directory')?>/images/no_image.gif" class="avatar avatar-32 photo" height="40" width="40" style="/* padding-left: 122px; */"> 
                                                          <?php
                                                        }else { ?>
                                                          <img alt="<?php echo $post->name_may;?>" src="<?php echo get_bloginfo('template_directory').'/upload_may/images/'.$post->image_may_url;?>" class="avatar avatar-32 photo" height="40" width="40" style="/* padding-left: 122px; */"> 
                                                          <?php } ?>
                                                          </td>
                                                        

                                                               
                                                           <td class="name column-name" style="text-align: left;font-weight: bold;"><?php echo $post->name_may;?></td>

                                                            <td class="email column-email" style="display: inline-block;width: 297px;">
                                                                   <form><fieldset></fieldset></form>
                                                           <div style="display: inline-block;width: 300px;padding-top: 8px;">
                                                            <div style='float:left;'>
                                                                   <form action='<?php bloginfo('url');?>/edit-product' method='POST'>
                                                                      <fieldset>
                                                                        <input type="hidden" name="edit_prod" id="edit_prod" value='<?php echo $post->id_may; ?>'>
                                                                        <input type="submit" name="edit_<?php echo $post->id_may;?>" id="edit_<?php echo $post->id_may;?>" value='Sửa'/>
                                                                      </fieldset>
                                                                  </form>
                                                            </div>
                                                            <div style='float:left;'>
                                                                <form id='xoa-form' method='POST'>
                                                                      <fieldset>
                                                                        <nav role="tab">
                                                                          <ul>
                                                                            <li idxoa='<?php echo $post->id_may; ?>'>
                                                                     <input 
                                                                     type="button" name="xoa" id="xoa" value='Xóa' 
                                                                     idxoa='<?php echo $post->id_may;?>' 
                                                                     name='idxoa<?php echo $post->id_may;?>' onclick="return confirm('bạn có chắc chắn muốn xóa máy này ? ');"
                                                                     />
                                                                            </li>
                                                                          </ul>
                                                                        </nav>
                                                                      <!-- onclick="if (confirm('Bạn muốn xóa máy này ? chọn Click OK . Nếu bạn muốn xóa chọn HỦY')) return true;" -->
                                                                      </fieldset>
                                                                  </form>
                                                            </div><!-- style='float:left;' -->
                                                            <div style='float:left;'>
                                                             <form action='<?php bloginfo('url');?>/manager-product-set?id=<?php echo $post->id_may;?>' method='POST'>
                                                              
                                                          <input type='hidden' name='phanquyen' id='phanquyen' value='<?php echo $post->id_may;?>'/>
                                                          <input  class="edit" type='submit' name='phanquyen_sumit' value='Phân quyền'/>
                                                          
                                                            </form>
                                                          </div>

                                                          </div>

                                                            </td>


                                                         
                                                       </tr><!-- id="user-xyz" class="alternate" -->

                                                      <?php } // end foreach($rows as $post) ?>


                                                       </tbody>
                                                      </table>
                  
                             </form>
                   <script type="text/javascript">
                  
                     jQuery(document).ready(function(){

                          $("nav[role='tab'] ul li").click(function() {
                          var id_xoa = $(this).attr("idxoa");
                         // var UrL  = "<?php bloginfo('url');?>/quan-li-may";
                         
                        //  alert(id_xoa);
                         
                         // return;

                          $.ajax({
                                  url:"<?php bloginfo('url');?>/processing-delete-product",
                                    type:"POST",
                                    data: {id_xoa:id_xoa},
                                    success: function(data)
                                    {
                                      //alert(data);
                                    $('#quanlimay').html(data);
                                    $('#quanlimay').show();
                                     //window.location.href('quan-li-may');
                                    }
                              });
                          });
                            
                        })
                    </script>
                   
                  </div> <!-- id='phuc' -->

               


                </div><!-- id="content_nano_two" -->
          <?php
        } // end neu la admin
    ?>

<?php 
} //  elseif ( ( $current_user->roles[0] != 'author') or ( $current_user->roles[0] != 'contributor') or ( $current_user->roles[0] != 'subscriber') )
} //elseif ( ! current_user_can( 'list_users' ) )  ?>
    
<?php get_footer(); ?>